/* ============================================================================= */
/*                   TABLE 10 - FINTECH VS BANK BORROWERS                        */
/* ============================================================================= */

clear all

/* ============================================================================= */
/*                   STEP 1: PREPARE WEIGHTED SAMPLES FOR MATCHING               */
/* ============================================================================= */

/* Load matched sample weights */
use "$output\matched_sample_nn.dta", clear
keep siren weight
sort siren weight
bys siren weight: gen nb = _n  // Identify unique firms
keep if nb == 1  // Keep only the first occurrence
drop nb
save "$output\matched_sample_nn_weight.dta", replace

/* Load default sample weights - low interest rate firms */
use "$output\table_default_low_rate.dta", clear
keep siren weight
sort siren weight
bys siren weight: gen nb = _n
keep if nb == 1
save "$output\table_default_low_rate_weight.dta", replace

/* Load default sample weights - high interest rate firms */
use "$output\table_default_high_rate.dta", clear
keep siren weight
sort siren weight
bys siren weight: gen nb = _n
keep if nb == 1
save "$output\table_default_high_rate_weight.dta", replace

/* ============================================================================= */
/*                 STEP 2: LOAD AND CLEAN CIPE DEFAULT DATA                      */
/* ============================================================================= */

/* Import CIPE data */
import delimited "$input\CIPE_SIREN_defaulters_mdate.csv", clear

/* Convert date formats */
gen time = date(date, "YMD")
gen qdate = qofd(time)  // Convert to quarterly date format
format time %td
format qdate %tq

/* Reclassify industry groups */
replace industry = "public" if inlist(industry, "P", "Q", "R", "S")  // Merge public sector
replace industry = "DE" if inlist(industry, "D", "E")  // Merge manufacturing industries
replace industry = "FL" if inlist(industry, "F", "L")  // Merge construction & real estate

/* Remove industries with insufficient observations */
drop if inlist(industry, "A", "K", "O", "B", "DE", "0000Z")

/* Create grouped industry variable */
egen industry10 = group(industry)

/* ============================================================================= */
/*                  STEP 3: STANDARDIZE RATINGS FOR ANALYSIS                     */
/* ============================================================================= */

/* Normalize ratings (converting different formats into a unified scale) */
replace rating = "3A" if rating == "3++"
replace rating = "3B" if rating == "3+"
replace rating = "3C" if rating == "3"
replace rating = "4A" if rating == "4+"
replace rating = "4B" if rating == "4"
replace rating = "5A" if rating == "5+"
replace rating = "5B" if rating == "5"

/* Assign numerical values to ratings */
gen ratinge = 13 if rating == "0"
replace ratinge = 1 if rating == "3A"
replace ratinge = 2 if rating == "3B"
replace ratinge = 3 if rating == "3C"
replace ratinge = 4 if rating == "4A"
replace ratinge = 5 if rating == "4B"
replace ratinge = 6 if rating == "5A"
replace ratinge = 7 if rating == "5B"
replace ratinge = 8 if rating == "6"
replace ratinge = 9 if rating == "7"
replace ratinge = 10 if rating == "8"
replace ratinge = 11 if rating == "9"
replace ratinge = 12 if rating == "P"

/* Set time series format */
tsset siren qdate, quarterly

/* Define firm defaults as bankruptcy (pclliquidate) or closure (pclopen) */
gen dead = pclliquidate | pclopen
replace dead = 0 if dead == .

/* Identify new loans */
gen newloan = loan > 0
replace newloan = 0 if loan == .

/* Keep data within the study period */
keep if inrange(qdate, qofd(date("20140101","YMD")), qofd(date("20191231","YMD")))

/* Start logging */
capture log close
log using "$logs\CIPE_and_PCL.log", replace

/* ============================================================================= */
/*              STEP 4: MERGE CIPE LOAN DATA AND CREATE EVENT TIME               */
/* ============================================================================= */

preserve
keep siren loan qdate
keep if loan > 0  // Keep firms that received a loan
rename qdate qdateloan
keep siren qdateloan
save "$output\CIPE_qdateloan.dta", replace
restore

/* Merge with main dataset to get loan dates */
merge m:1 siren using "$output\CIPE_qdateloan.dta"
keep if _merge == 1 | _merge == 3  // Keep firms with loan data
drop _merge

/* Compute time difference (years) since the loan was granted */
gen delta_year = yofd(dofq(qdate)) - yofd(dofq(qdateloan))
keep if inrange(delta_year, -4, 4)  // Keep only observations within -4 to +4 years

/* Identify high-rate loans */
preserve
keep if qdate == qdateloan - 1
gen unrated_pre = ratinge == 13
egen dum_rate = xtile(rate), n(2) by(p2p)  // Split rates into two groups
gen high_rate = dum_rate == 2
keep siren unrated_pre high_rate
save "$output\unrated_siren.dta", replace
restore

/* Merge high-rate loan indicator */
merge m:1 siren using "$output\unrated_siren.dta", nogen keep(3)

/* Define post-loan period */
gen post = qdateloan < qdate

/* Create firm-level fixed effects */
egen nesc = group(industry)
gen year = year(time)
tsset siren qdate, quarterly

/* Merge with matched sample weights */
merge m:1 siren using "$output\matched_sample_nn_weight.dta"
keep if _merge == 3
drop _merge

/* ============================================================================= */
/*               STEP 5: REGRESSION ANALYSIS - DEFAULT RISK                      */
/* ============================================================================= */

/* Firm-level default regressions */
capture estimates drop *

eststo reg1: reghdfe dead i1.p2p##i1.post [pweight=weight], a(i.siren i.nesc#i.qdate) cluster(siren) 
estadd local firm "Y", replace 
estadd local indyear "Y", replace 

eststo reg2: reghdfe dead i1.p2p##i1.post##i.high_rate [pweight=weight], a(i.siren i.nesc#i.qdate) cluster(siren) 
estadd local firm "Y", replace 
estadd local indyear "Y", replace 

/* Default regressions for low-rate firms */
preserve
drop weight
merge m:1 siren using "$output\table_default_low_rate_weight.dta", nogen keep(3)
eststo reg2l: reghdfe dead i1.p2p##i1.post [pweight=weight], a(i.siren i.nesc#i.qdate) cluster(siren) 
estadd local firm "Y", replace 
estadd local indyear "Y", replace 
restore

/* Default regressions for high-rate firms */
preserve
drop weight
merge m:1 siren using "$output\table_default_high_rate_weight.dta", nogen keep(3)
eststo reg2h: reghdfe dead i1.p2p##i1.post [pweight=weight], a(i.siren i.nesc#i.qdate) cluster(siren)
estadd local firm "Y", replace 
estadd local indyear "Y", replace 
restore

/* Rating regressions */
eststo reg3: reghdfe ratinge i1.p2p##i1.post [pweight=weight] if ratinge != 13, a(i.siren i.nesc#i.qdate) cluster(siren) 
estadd local firm "Y", replace 
estadd local indyear "Y", replace 

/* Rating regressions with high-rate interaction */
eststo reg4: reghdfe ratinge i1.p2p##i1.post##i.high_rate [pweight=weight] if ratinge != 13, a(i.siren i.nesc#i.qdate) cluster(siren) 
estadd local firm "Y", replace 
estadd local indyear "Y", replace 

preserve
drop weight
merge m:1 siren using "$output\table_default_low_rate_weight.dta", nogen keep(3)
eststo reg4l: reghdfe ratinge	i1.p2p##i1.post [pweight=weight] if ratinge !=13, a(i.siren i.nesc#i.qdate) cluster(siren) 
estadd local firm "Y", replace 
estadd local indyear "Y", replace 
restore

preserve
drop weight
merge m:1 siren using "$output\table_default_high_rate_weight.dta", nogen keep(3)
eststo reg4h: reghdfe ratinge	i1.p2p##i1.post [pweight=weight] if ratinge !=13, a(i.siren i.nesc#i.qdate) cluster(siren) 
estadd local firm "Y", replace 
estadd local indyear "Y", replace 
restore

/* Export Table 10 - Col 1 2 3 */
esttab reg1 reg2l reg2h using "$tables\Table_firm_level_defaults_matched.tex", replace ///
    noomitted nobase fragment gap booktabs label nonote noobs nolines nomtitles nonumber /// 
    b(%9.3f) drop(_cons) se(%9.3f) star(* 0.10 ** 0.05 *** 0.01) ///
    prehead("\multicolumn{3}{c}{Default} & \multicolumn{3}{c}{Rating}") ///
    order(1.p2p#1.post 1.post) varlabel(1.p2p#1.post `"FinTech $\times$ Post"' ' 1.post "Post") ///
    stats(firm indyear N r2, label(`"Firm FE"' `"Industry-Quarter FE"' `"\midrule N"' `"R-sq"')) 

/* Export IA Table G1 - Col 1 2 3 */
esttab reg3 reg4l reg4h using "$tables\Table_firm_level_ratings_matched.tex", replace ///
    noomitted nobase fragment gap booktabs label nonote noobs nolines nomtitles nonumber /// 
    b(%9.3f) drop(_cons) se(%9.3f) star(* 0.10 ** 0.05 *** 0.01) ///
    prehead("\multicolumn{3}{c}{Default} & \multicolumn{3}{c}{Rating}") ///
    order(1.p2p#1.post 1.post) varlabel(1.p2p#1.post `"FinTech $\times$ Post"' ' 1.post "Post") ///
    stats(firm indyear N r2, label(`"Firm FE"' `"Industry-Quarter FE"' `"\midrule N"' `"R-sq"')) 
	
log close
